{
 "cells": [
  {
   "source": [
    "## 0. 下载Tushare"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install tushare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts \n",
    "ts.set_token(\"****************************\") \n",
    "# 引号内为你自己的Token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pro = ts.pro_api() # 设置Tushare Pro的初始化接口，并赋值为pro"
   ]
  },
  {
   "source": [
    "## 1. 获取股票的基本信息"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "`pro.stock_basic()`接口能够获取股票代码、股票名称、所在地域、所属行业以及上市日期。键入以下命令："
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "basic = pro.stock_basic()\n",
    "basic "
   ]
  },
  {
   "source": [
    "`pro.stock_basic()`接口也能通过`fields`选项选择自己需要的参数。如我们需要获取股票的英文名称和交易货币，那么我们可以通过`fields`选项下的参数`enname`和`curr_type`进行获取："
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "specific = pro.stock_basic(fields = 'enname, curr_type')\n",
    "specific "
   ]
  },
  {
   "source": [
    "## 2. 获取日线行情数据"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "### 2.1 ts.get_today_all()"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "`ts.get_today_all()`成员函数可以一次性获取当前交易的所有股票的行情数据（如果是节假日，即为上一交易日数据）。基本使用方法如下："
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "today = ts.get_today_all()\n",
    "today\n",
    "\n",
    "# 变量名从左至右依次为：代码（code），名称（name），涨跌幅（changepercent），现价（trade），开盘价（open），最高价（high），最低价（low），收盘价（settlement），成交量（volume），换手率（turnoverratio），成交额（amount），市盈率（per），市净率（pb），总市值（mktcap），流通市值（nmc）。"
   ]
  },
  {
   "source": [
    "### 2.2 pro.daily()"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "使用`pro_daily()`接口同样可以获取日线数据，方法如下："
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "daily = pro.daily()\n",
    "daily\n",
    "\n",
    "# 从左至右分别表示股票代码（ts_code），交易日期（trade_date），开盘价（open），最高价（high\t），最低价（low），收盘价（close），昨日收盘价（pre_close），涨跌额（change），涨跌幅（pct_chg），成交量/手（vol），成交额/千元（amount）"
   ]
  },
  {
   "source": [
    "### 2.3 二者区别"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "与`ts.get_today_all()`不同的是，`pro.daily()`可以选择获取单只股票，或多只股票"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "> `pro.daily()`接口获取单只股票数据，股票代码为'000001.SZ'，起始日期为2021.01.01，终止日期为2021.06.30"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "daily_single = pro.daily(ts_code = '000001.SZ', start_date = '20210101', end_date = '20210630')\n",
    "daily_single"
   ]
  },
  {
   "source": [
    "> `pro.daily()`接口获取多只股票数据，股票代码分别为'000002.SZ'和'600000.SH'，起始日期为2021.01.01，终止日期为2021.06.30"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "daily_multi = pro.daily(ts_code = '000002.SZ, 600000.SH', start_date = '20210101', end_date = '20210630')\n",
    "daily_multi"
   ]
  },
  {
   "source": [
    "## 3. 获取实时分笔数据"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    ">在Tushare中，可以使用`ts.get_realtime_quotes()`函数实时获取股票当前报价和成交信息，从而实时监测交易量和价格的变化。"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    "### 3.1 显示全部属性"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = ts.get_realtime_quotes('000001')\n",
    "df1"
   ]
  },
  {
   "source": [
    "### 3.2 显示部分属性"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = ts.get_realtime_quotes('000002')   \n",
    "df2[['code', 'name', 'price', 'bid', 'ask', 'volume', 'amount', 'time']]  #选择需要的属性\n",
    "\n",
    "# 返回的结果从左至右分别为股票代码（code），股票名称（name），当前价格（price），竞买价（bid），竞卖价（ask），成交量（volume），成交金额（amount）以及当前时间（time）。"
   ]
  },
  {
   "source": [
    "### 3.3 同时获取多只股票数据"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    ">如果想同时获取多只股票的实时交易数据，可以将股票代码写入列表中，然后将整个列表作为参数传入："
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df3 = ts.get_realtime_quotes(['600900', '000002','600519'])\n",
    "df3[['code', 'name', 'price', 'bid', 'ask', 'volume', 'amount', 'time']]"
   ]
  },
  {
   "source": [
    "### 3.4 获取股票指数数据"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "source": [
    ">除此之外，我们还可以传入字符串形式的股票指数代码，如：'sh'=上证指数，'sz'=深圳成指，'hs300'=沪深300指数，'sz50'=上证50指数，'zxb'=中小板指数，'cyb'=创业板指数。"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取上证指数\n",
    "\n",
    "sh = ts.get_realtime_quotes('sh')\n",
    "sh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 同时获取上证指数，深圳成指，中小板指数及创业板指数\n",
    "sh_sz_sxb_cyb = ts.get_realtime_quotes(['sh', 'sz', 'zxb', 'cyb'])  \n",
    "sh_sz_sxb_cyb[['name', 'pre_close', 'open', 'price', 'high', 'low', 'bid', 'ask', 'volume', 'amount', 'time']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "ce9d3308a83a37d22649dcd50c19e181fcfb7887b13f1c6f31fcc7446dbaf41b"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}